package com.redis.topic;

import org.redisson.api.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Component
public class TopicListener2 {

    @Resource
    private RedissonClient redissonClient;

    @PostConstruct
    public void Listener(){

        //发布订阅
        RTopic<String> topic = redissonClient.getTopic("anyTopic");
        topic.addListener((channel, msg) -> System.out.println(msg));
        System.out.println("anyTopic被监听");

        //限流器
        RRateLimiter rateLimiter = redissonClient.getRateLimiter("myRateLimiter");
        // 初始化
        // 最大流速 = 每1秒钟产生10个令牌
        boolean b = rateLimiter.trySetRate(RateType.PER_CLIENT, 10, 1, RateIntervalUnit.SECONDS);
        System.out.println("限流器初始化"+b);
    }

}
